'''
Created on 24/03/2009

@author: eh01
'''
from BaseHTTPServer import HTTPServer
from im.webserver.handler.imhttprequesthandler import IMHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading


class MultiThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    pass

class IMHTTPServer(threading.Thread):
    '''
    IMHTTPServer creates a HTTPServer in a new thread
    '''
    __server = None
    __port = None

    def __init__(self, port):
        '''
        Constructor
        '''
        self.__port = port
        threading.Thread.__init__(self)
    
    def run(self):
        self.__server = MultiThreadedHTTPServer(('', self.__port), IMHTTPRequestHandler)
#        self.__server = HTTPServer(('', self.__port), IMHTTPRequestHandler)
        self.__server.serve_forever()
    
    def stop(self):
        self.__server.shutdown()
        self.__server.socket.close()
        print 'Stopped Server'